package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.pojo.Page;
import com.itheima.service.CategoryService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.ValidationEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author wuyiqiang
 * @2022/6/14 13:37
 */
@RestController
@RequestMapping(value = "/category",produces = "application/json;charset=utf-8")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增分类
     *
     * @param request
     * @param category
     * @return
     */
    @PostMapping
    public R add(HttpServletRequest request, @RequestBody Category category) {
        //设置id
        String categoryId = System.currentTimeMillis() + "";
        category.setId(categoryId);
        //获取当前时间并写入
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");
        Date date = new Date();
        //设置创建时间
        category.setCreateTime(date);
        //设置更新时间
        category.setUpdateTime(date);
        //设置创建人和修改人的id
        String username = tokenToUsername(request);
        category.setUpdateUser(username);
        category.setCreateUser(username);

        try {
            int add = categoryService.add(category);
            if (add > 0) {
                return R.success(category);
            }
        } catch (Exception e) {
            System.out.println("菜品新增======================debug");
        }
        return R.error(category.getName() + "已存在");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(@Param("page") Integer page, @Param("pageSize") Integer pageSize) {
        return categoryService.page(page, pageSize);
    }

    /**
     * 下拉查询
     *
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R dropDown(@Param("type") Integer type) {
        List<Category> list = categoryService.dropDown(type);
        return R.success(list);
    }

    /**
     * 删除分类信息
     * @param id
     * @return
     */
    @DeleteMapping
    public R delete(@Param("id") String id) {
        return categoryService.delete(id);
    }

    /**
     * 修改分类信息
     * @param request
     * @param category
     * @return
     */
    @PutMapping
    public R update(HttpServletRequest request,@RequestBody Category category){
        String username = tokenToUsername(request);
        category.setUpdateUser(username);
        //获取当前时间并写入
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");
        Date date = new Date();
        category.setUpdateTime(date);

        categoryService.update(category);
        return R.success("修改分类信息成功");
    }

    /**
     * 利用token获得当前页面用户名
     *
     * @param request
     * @return
     */
    public String tokenToUsername(HttpServletRequest request) {
        //用户id设置
        String token = request.getHeader("Authorization");

        String username = (String) redisTemplate.opsForValue().get(token);

        return username;
    }
}
